package com.yjt.fresh.activity;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.CountDownTimer;
import android.util.DisplayMetrics;
import android.util.Log;

import com.yjt.fresh.MainActivity;
import com.yjt.fresh.R;
import com.yjt.fresh.base.BaseSimpleActivity;

import java.util.ArrayList;

/**
 * 启动页
 * Created by mcc on 2018/6/10.
 */

public class AtyStart extends BaseSimpleActivity {


    private final int SDK_PERMISSION_REQUEST = 127;
    private boolean isTimeFinish;
    private boolean isLocationFineComplete;
    private boolean isLocationCoarseComplete;


    @Override
    protected int getContentView() {
        return R.layout.app_aty_start;
    }

    @Override
    protected void initViews() {
        getPersimmions();
        new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long l) {

            }

            @Override
            public void onFinish() {
                isTimeFinish = true;
                if (isLocationCoarseComplete && isLocationFineComplete) {
                    startActivity(MainActivity.class);
                    finish();
                }
            }
        }.start();
    }

    @Override
    protected void initEvent() {

    }

    @TargetApi(23)
    private void getPersimmions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList<String> permissions = new ArrayList<String>();
            /***
             * 定位权限为必须权限，用户如果禁止，则每次进入都会申请
             */
            // 定位精确位置
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
            } else {
                isLocationFineComplete = true;
            }
            if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
            } else {
                isLocationCoarseComplete = true;
            }
            if (permissions.size() > 0) {
                requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST);
            }
        } else {
            isLocationCoarseComplete = true;
            isLocationFineComplete = true;
        }
    }

    @TargetApi(23)
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        isLocationCoarseComplete = true;
        isLocationFineComplete = true;
        if (isTimeFinish) {
            startActivity(MainActivity.class);
            finish();
        }

    }
}